<?php

namespace App\Repository\User;

use App\Model\User\User;
use App\Repository\IRepository;
use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Database\Model\Builder;

/**
 * 用户管理 Repository类
 */
class UserRepository extends IRepository
{
    public function __construct(
        protected readonly User $model,
    )
    {
    }

    /**
     * 搜索器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        $query->when(isset($params['nickname']) && $params['nickname'], function ($query) use ($params) {
            return $query->where('nickname', 'like', '%' . $params['nickname'] . '%');
        });
        $query->when(isset($params['mobile']) && $params['mobile'], function ($query) use ($params) {
            return $query->where('mobile', 'like', '%' . $params['mobile'] . '%');
        });
        $query->when(isset($params['status']) && $params['status'], function ($query) use ($params) {
            return $query->where('status', $params['status']);
        });
        return $query;
    }


    public function handlePage(LengthAwarePaginatorInterface $paginator): array
    {
        return [
            'total' => $paginator->total(),
            'per_page' => $paginator->perPage(),
            'current_page' => $paginator->currentPage(),
            'list' => $paginator->items(),
        ];
    }
}